package com.wenbing.miio.core;

import java.util.Set;

/**
 * @author wen bing
 * @projectName: miio
 * @package: core.ps
 * @className: P
 * @date 2024/09/19 11:07
 **/
public interface Context {

    /**
     * 获取私有属性，只允许获取该层属性
     * @param key
     * @param attr
     */
    void setAttribute(String key, Object attr);

    default <T> T getAttribute(String key) {
        return getAttribute(key, null);
    }

    <T> T getAttribute(String key, T defVal);

    void removeAttribute(String key);

    Set<String> keySet();

    static void copy(Context from, Context to, boolean override) {
        assert from != null && to != null;
        for (String key : from.keySet()) {
            Object attrValue = to.getAttribute(key);
            if (attrValue == null || override) {
                to.setAttribute(key, from.getAttribute(key));
            }
        }
    }
}
